﻿'''
Research Staff:    
    sumnieng ongsupankul
    chakkree tiyawongsuwan
    jirat bunjongsiri
    
date: 2010 June 11
      แยกคลาสออกเป็นไฟล์
      
'''
from svgfig import *

#import sys , os
#sys.path.append( os.path.join( os.getcwd(), '..' ) )

#from SVGDrawingUtility import *

'''
 หน่วยเป็น มม
 
'''
micronorm = [0.13,0.18,0.25,0.35,0.5,0.7,1.0,1.4]


'''
   ใช้แสดงผลงาน
   ไดเติ้ลบล็อก ของ มหาวิทยาลัยเทคโนโลยีราชมงคลรัตนโกสินทร์.
'''
class TitleBlockRMUTR(object):
    def __init__(self):
        self.x = 10
        self.y = 10
        self.width=210-20
        self.height = 25

        self.logoName = 'logo_RMUTR.jpg'
        self.organization = u"มหาวิทยาลัยเทคโนโลยีราชมงคลรัตนโกสินทร์"        
        #self.organization ='Rajamangala University of Technology Rattanakosin'
        
    def __repr__(self):
        return "<TitleBlock x='%g' y='%g' logoName='%s'>" %( \
            self.x,self.y,self.logoName)
    
    def SVG(self):
        border = SVG("rect" , x='0' , y='0' , \
                     width='%g'%self.width , height='%g'%self.height ,
                     fill='white')


        logo = SVG("image" , x='2' ,y='2' ,width='11', height="20")
        logo.attr["xlink:href"] = self.logoName

        

        text1 = SVG("text" ,  self.organization ,x='15' , y='8' , \
                   font_size='6', stroke="none", fill="black", text_anchor="start" )
        workName = u"งานวิจัย : การพัฒนาภาษาไพธอนสำหรับเขียนแบบก่อสร้างชิ้นส่วนโครงสร้างคอนกรีตเสริมเหล็ก"
        text2 = SVG("text", workName  ,x='15' , y = '12' , \
                   font_size='3.5', stroke="none", fill="black", text_anchor="start")
        attr3 = ''
        text3 = SVG("text" , \
                    SVG("tspan" , u"ดร.สำเนียง องสุพันธ์กุล" , x='27' ) ,\
                    SVG("tspan" , u"อ.จักรี ติยะวงศ์สุวรรณ" ,x='27' , dy='3') ,\
                    SVG("tspan" , u"ผศ.จิรัฐิติ์ บรรจงศิริ"   ,x='27', dy='3') ,
                    y='15' , \
                    font_size='2.5', stroke="none", fill="black", text_anchor="start")

        
        
        titleSVG = SVG("g" , border ,logo ,text1 , text2 ,text3 )        
        titleSVG.attr["transform"] = 'translate(%g,%g)' %(self.x,self.y)
        
        return titleSVG


def GridGraph(x,y,width,height):
    myName ='GridGraph'
    GridSVG = SVG('g', id=myName)
    # vertical grid
    xi = 10
    while (xi<width):
        GridSVG.append(SVG('line', \
            x1='%g'%xi , y1 ='0' ,x2='%g'%xi , y2='%g'%(height) ,
            style= "stroke:lightblue;stroke-width:0.13"))
        xi+=10
    # horizontal grid
    yi = 10
    while (yi<height):
        GridSVG.append(SVG('line', \
            x1='0' , y1 ='%g'%yi ,x2='%g'%width , y2='%g'%yi ,
            style= "stroke:lightblue;stroke-width:0.13"))
        yi+=10
        

    GridSVG.attr['transform'] = 'translate(%g,%g)'%(x,y)
    return GridSVG

'''
date : 2010 Aug 8
   - แก้ไขให้ class paper มีการรับ dict ของ arg
'''


class paper(object):
    '''
        สร้างวัตถุ SVG ขนาดกระดาษต่าง ๆ
    '''
    default = {
        'size' : "A4" , 
        'orientation' : 'portrait' ,
        'showGrid' : True
    }
    def __init__(self, **attr):
        #self.size="A4"                   # A4 , A3 , A2 
        #self.orientation = "portrait"   # portrait  , landscape
        
        attributes = dict(self.default)
        attributes.update(attr)
        attributes = attr_preprocess(attributes)
        
        size = attributes['size']
        orientation = attributes['orientation']
        self.showGrid = attributes['showGrid']

        self.SVGoutput = SVG("g")
        #print size , orientation
        self.setPaper(size , orientation)        


        
    def setPaper(self , size , orientation):
        self.size = size
        print size , orientation
        if self.size=="A4":
            self.width , self.height = 210 , 297
        elif self.size=="A3":
            self.width , self.height = 297 , 420
            print "Pass Set A3 Paper"            
        elif self.size=="A2":
            self.width , self.height = 420 , 594
        elif self.size=="A1":
            self.width , self.height = 594 , 840    
        else:
            print "Error: paper %s size not correct " %  self.size

        # set orientation
        self.orientation = orientation
        if self.orientation == "portrait":
            if self.width > self.height:
                tmp = self.height
                self.height = self.width
                self.width  = tmp
        elif self.orientation == "landscape":
            if self.height > self.width:
                tmp = self.height
                self.height = self.width
                self.width  = tmp
        else:
            print "Error: orientation %s not found " %  self.orientation
            
        print self.width , self.height
        if self.showGrid:
            self.prepend(GridGraph(10,10,self.width-20,self.height -20))

    def prepend(self,objectSVG):
        self.SVGoutput.prepend(objectSVG)
        
    def append(self,objectSVG):
        self.SVGoutput.append(objectSVG)
    
        
    def xml(self):
        return self.SVGoutput.xml()
        
    def save(self,FileName):
        #width = self.width
        #height = self.height
        
        border = SVG("rect" , x='10' , y='10' , \
                 width='%g'%(self.width-20),height='%g'%(self.height-20))
        #self.SVGoutput.append(border)
        
        canv = canvas(border , \
                     width='%dmm'%self.width  , height='%dmm'%self.height)
        canv.attr["viewBox"] = "0 0 %d %d" % ( self.width , self.height )\
        # สำหรับ Hack Pattern จาก Inkscape
        canv.attr['xmlns:inkscape']="http://www.inkscape.org/namespaces/inkscape"
        canv.prepend(self.SVGoutput)

        # ดูวิธีตั้งค่า canvas ได้ที่ http://code.google.com/p/svgfig/wiki/DefCanvas
        canv.attr['style'] ='''stroke:black; fill:none; stroke-width:0.13mm; stroke-linejoin:round; text-anchor:start'''
        canv.attr['font-family'] = "Browallia New" 
        canv.save(FileName,'utf-8')        



      
if __name__=="__main__":

    title1 = TitleBlockRMUTR()
    #portrait  , landscape
    Paper = paper(size="A4" , orientation="portrait" , showGrid=True)
    #Paper.append(title1.SVG())
    
    
    pathname = os.path.dirname(sys.argv[0])
    fileName = pathname + "/" + "SamplePaperA4P.svg"
    Paper.save(fileName)


